# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=x264
# Upstream doesn't do releases; track branch "stable"
#   https://code.videolan.org/videolan/x264/-/tree/stable.
# The minor version corresponds the ABI version. If it needs to be bumbed,
# abuild will alert you.
# The patch versions is the count of commits on the stable branch (see version.sh).
#   git rev-list --count stable
pkgver=0.164.3108
_gitrev=31e19f92f00c7003fa115047ce50978bc98c3a0d
pkgrel=0
pkgdesc="Free library for encoding H264/AVC video streams"
url="https://www.videolan.org/developers/x264.html"
arch="all"
license="GPL-2.0-or-later"
makedepends="
	bash
	coreutils
	libx11-dev
	nasm
	perl
	"
subpackages="$pkgname-dev $pkgname-libs $pkgname-bash-completion"
source="$pkgname-$_gitrev.tar.gz::https://code.videolan.org/videolan/x264/-/archive/$_gitrev/x264-$_gitrev.tar.gz
	version.patch
	incompatible-pointer.patch
	"
builddir="$srcdir/$pkgname-$_gitrev"

prepare() {
	default_prepare
	update_config_sub

	local abi; abi=$(sed -n 's/^#define X264_BUILD \([1-9][0-9]*\).*$/\1/p' x264.h)
	if [ "${pkgver%.*}" != "0.$abi" ]; then
		error "ABI version has been changed to $abi!"
		error "Bump pkgver to 0.${abi}.${pkgver##*.} and rebuild all dependent packages"
		return 1
	fi
}

build() {
	local asmopts=""
	case "$CARCH" in
		# x86 assembly contains TEXTRELs
		# armhf/armv7: Illegal instruction
		x86|armhf|armv7) asmopts="--disable-asm";;
	esac

	# note: not autotools
	pkgver=$pkgver \
	CFLAGS="${CFLAGS/-Os/} -flto=auto" ./configure \
		--host=$CHOST \
		--prefix=/usr \
		--enable-lto \
		--enable-shared \
		--enable-static \
		$asmopts \
		--enable-pic
	make
}

check() {
	./x264 --version | grep -q "$pkgver"
}

package() {
	make DESTDIR="$pkgdir" install

	mkdir -p "$pkgdir"/usr/share/bash-completion/completions
		mv "$builddir"/tools/bash-autocomplete.sh \
		"$pkgdir"/usr/share/bash-completion/completions/x264
}

sha512sums="
707ff486677a1b5502d6d8faa588e7a03b0dee45491c5cba89341be4be23d3f2e48272c3b11d54cfc7be1b8bf4a3dfc3c3bb6d9643a6b5a2ed77539c85ecf294  x264-31e19f92f00c7003fa115047ce50978bc98c3a0d.tar.gz
d3eb0294a7beb36d26fb068af3220b7e24b95799be4aa39120344d439b160311e3e8e69c5b3dc1b8e747e3d46c38cd8af619470ff9375aa84ae6cc652c527d23  version.patch
b5e834bd542cc67ff14e46820d9dd379a2f0148a217d0118a8ee6fba29da6461fc15370bbad09481f0391f579ca7a4271acd157287090e1ed0e4fb1018f230fb  incompatible-pointer.patch
"
